﻿using System;
using System.IO;
using System.Net;

namespace ConsoleApplication1
{
    class HttpTest
    {
        public static void GetTest()
        {
            string data;
            //loginParams = "username=" + logInUserIdString + "&password=" + logInPasswordString;
            //string url = "http://www.baidu.com/";
            string url = "http://localhost:49608/Action/ProductAdd.aspx";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.Headers.Add("key1","v1");
            request.Headers.Add("key2", "v2");

            string paraUrlCoded = System.Web.HttpUtility.UrlEncode("oq");
            paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode("request no parameter");
            //paraUrlCoded += "& " + System.Web.HttpUtility.UrlEncode("VIN");
            //paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(txtCarNumber.Text.Trim()); 

            byte[] payload;
            //将URL编码后的字符串转化为字节  
            payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
            //设置请求的ContentLength   
            request.ContentLength = payload.Length;
            //获得请求流  
            Stream writer = request.GetRequestStream();
            //将请求参数写入流  
            writer.Write(payload, 0, payload.Length);
            //关闭请求流  
            writer.Close();  

            //request.Accept = "application/json;odata=verbose";
            //HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
            HttpWebResponse response = (HttpWebResponse) request.GetResponse();
            using (var sr = new StreamReader(response.GetResponseStream()))
            {
                data = sr.ReadToEnd();
            }

            Console.WriteLine(data);
        }

    }
}
